'use strict';

const Controller = require('egg').Controller;

class WebController extends Controller {
  async index() {
    const { ctx } = this;
    var { limit = 10, page = 1, } = ctx.request.body;
    let data = {
      where: { isdel: 0 }, // WHERE 条件
      columns: ['id', 'title', 'cover', 'content', 'con_info'], // 要查询的表字段
      limit: limit, // 返回数据量
      offset: limit * (page - 1), // 数据偏移量
    }
    const list = await this.app.mysql.select('article', data);
    delete data.limit
    delete data.offset
    const total = await this.app.mysql.select('article', data);
    ctx.body = {
      code: 200,
      data: list,
      total: total.length
    }
  }
  async info() {
    const { ctx } = this;
    var { id } = ctx.request.body;
    const list = await this.app.mysql.get('article', { id });
    ctx.body = {
      code: 200,
      data: list,
    }
  }
  async login() {
    const { ctx } = this;
    var { user, pwd } = ctx.request.body;
    ctx.body = {
      data: {
        user_id: user,
        auth_token: pwd,
        real_name: "Admin"
      },
      error: {
        code: 0,
        msg: "Login success"
      }
    };
  }
  async getmenu() {
    const { ctx } = this;
    var { user, pwd } = ctx.request.body;
    ctx.body = {
      data: [
        {
          path: "/",
          name: "首页",
          compName: "Dashboard",
          compPath: "/dashboard"
        },
        {
          path: "/hello",
          name: "Hello",
          children: [
            {
              path: "/hello/hello-world",
              name: "HelloWorld",
              compName: "HelloWorld",
              compPath: "/hello/helloWorld"
            }
          ]
        },
        {
          path: "/tool",
          name: "工具",
          children: [
            {
              path: "/tool/ToolJournal",
              name: "日志",
              compName: "ToolJournal",
              compPath: "/tool/ToolJournal"
            }
          ]
        },
        {
          path: "/goods",
          name: "产品",
          children: [
            {
              path: "/goods/goodsList",
              name: "商品列表",
              compName: "goodsList",
              compPath: "/goods/goodsList"
            },
            {
              path: "/goods/addGoods",
              name: "商品添加",
              compPath: "/goods/addGoods"
            },
          ]
        }
      ],
      error: {
        code: 0,
        msg: "Get menu success"
      }
    };
  }

  async journal() {
    const { ctx } = this;
    var { page } = ctx.request.body;

    ctx.body = {
      data: [
        {
          name: "John Brown",
          age: 18,
          address: "New York No. 1 Lake Park",
          date: "2016-10-03"
        },
        {
          name: "Jim Green",
          age: 24,
          address: "London No. 1 Lake Park",
          date: "2016-10-01"
        },
        {
          name: "Joe Black",
          age: 30,
          address: "Sydney No. 1 Lake Park",
          date: "2016-10-02"
        },
        {
          name: "Jon Snow",
          age: 26,
          address: "Ottawa No. 2 Lake Park",
          date: "2016-10-04"
        }
      ],
      error: {
        code: 0,
        msg: "Get test success"
      }
    };
  }
  async increaseGoods() {
    const { ctx } = this;
    var body = ctx.request.body;
    console.log(body)
    const data = await ctx.service.web.add(body);
    if (data.affectedRows == 1) {
      ctx.body = {
        error: {
          code: 0,
          msg: "success"
        }
      }
    } else {
      ctx.body = {
        error: {
          code: -1,
          msg: "fail"
        }
      }
    }
  }
  async getGoods() {
    const { ctx } = this;
    var body = ctx.request.body;
    const data = await ctx.service.web.getGoods(body);
    const total = await ctx.service.web.getTotal('product');


    ctx.body = {
      data,
      total,
      error: {
        code: 0,
        msg: "success"
      }
    }
  }



}

module.exports = WebController;
